package cn.iocoder.boot.utils;



import cn.hutool.crypto.digest.DigestUtil;

import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/***
 * iceasy帐号密码加密解密工具类
 */
public class ICEasyEncryptionUtil {





    public static void main(String[] args) throws Exception {
        String account = "kingbrother";
        String password = "Kiy3@da46aKpsfa_uHds";
        String txnTime = "20250425141920";

        // 加密
        String enString = ICEasyEncryptionUtil.encrypt(account,password, txnTime);
        System.out.println("加密后的字串是：" + enString);

        // 解密
        String DeString = ICEasyEncryptionUtil.decrypt(account,enString, txnTime);
        System.out.println("解密后的字串是：" + DeString);



    }


    /**
     *加密
     * <p>对于接口的密码类(密码，交易密码等),需要统一加密传输。先通过AES128加密，密钥是(用户id+交易时间[防止时间过期])的md5加密前16位,再MD5加密</p>
     * @param account 帐号
     * @param password 密码
     * @param txnTime 时间格式字符串，格式格式:yyyyMMddHHmmss 如：20200716141920
     * @return
     * @throws
     */
    public static String encrypt( String account,String password, String txnTime){

        try {
            String aes128Key = DigestUtil.md5Hex(account + txnTime).toUpperCase();
            return encrypt(password, aes128Key.substring(0,16));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 解密
     * @param account 帐号
     * @param key 加密后密钥
     * @param txnTime 时间格式字符串，格式格式:yyyyMMddHHmmss 如：20200716141920
     * @return
     */
    public static String decrypt( String account, String key,String txnTime){

        try {
            String aes128Key = DigestUtil.md5Hex(account + txnTime).toUpperCase();
            return decrypt(key, aes128Key.substring(0,16));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 加密
    public static String encrypt(String sSrc, String sKey) throws Exception {
        if (sKey == null) {
            System.err.print("Key为空null");
            return null;
        }
        // 判断Key是否为16位
        if (sKey.length() != 16) {
            System.out.print("Key长度不是16位");
            return null;
        }
        byte[] raw = sKey.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");// "算法/模式/补码方式"
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));

        return Base64.getEncoder().encodeToString(encrypted);// 此处使用BASE64做转码功能，同时能起到2次加密的作用。
    }

    // 解密
    public static String decrypt(String sSrc, String sKey) throws Exception {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.err.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                return null;
            }
            byte[] raw = sKey.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] encrypted1 = Base64.getDecoder().decode(sSrc);// 先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original, "utf-8");
                return originalString;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}